package cn.tedu.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.tedu.entity.User;
import cn.tedu.service.UserService;
import cn.tedu.service.impl.UserServiceImpl;
import cn.tedu.util.ResponseUtil;
import cn.tedu.vo.Result;

/**
 * Servlet implementation class UserLoginServlet
 */
public class UserLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.获取数据
		String name=request.getParameter("loginName");
		String pwd=request.getParameter("password");
		System.out.println(name+"   "+pwd);
		User user=new User();
		user.setLoginName(name);
		user.setPassword(pwd);
		
		//2.调用登录业务
		UserService userService=new UserServiceImpl();
		Result result=userService.login(user);	
		//3.根据业务的结果做跳转和响应,把result对象转化成json对象
		ResponseUtil.jsonToClient(response, result);
		
		/*response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		out.println("{\"message\":\"success\"}");//{"message":"success"}
		out.flush();
		out.close();*/
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
